package com.atguigu.gmall.service.impl;

import com.atguigu.gmall.entity.ProductSalePropertyKey;
import com.atguigu.gmall.entity.ProductSalePropertyValue;
import com.atguigu.gmall.mapper.SalePropertyKeyMapper;
import com.atguigu.gmall.service.SalePropertyKeyService;
import com.atguigu.gmall.service.SalePropertyService;
import com.atguigu.gmall.service.SalePropertyValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SalePropertyKeyServiceImpl extends ServiceImpl<SalePropertyKeyMapper, ProductSalePropertyKey> implements SalePropertyKeyService {

    @Autowired
    private SalePropertyValueService salePropertyValueService;

    @Override
    public List<ProductSalePropertyKey> querySalePropertyByProductId(Long productId) {
        QueryWrapper<ProductSalePropertyKey> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        List<ProductSalePropertyKey> list = baseMapper.selectList(queryWrapper);
        list.stream().forEach(item -> {
            //查询value，并封装
            packageReturnValue(productId,item);
        });
        return list;
    }

    //根据key_id 获取value
    private void packageReturnValue(Long productId,ProductSalePropertyKey salePropertyKey) {
        QueryWrapper<ProductSalePropertyValue> queryWrapper = new QueryWrapper<>();
        Long salePropertyKeyId = salePropertyKey.getSalePropertyKeyId();
        queryWrapper.eq("product_id", productId).eq("sale_property_key_id",salePropertyKeyId);
        List<ProductSalePropertyValue> list = salePropertyValueService.list(queryWrapper);
        salePropertyKey.setSalePropertyValueList(list);
    }
}
